#! /usr/bin/env python
# -*- coding: UTF-8 -*-

import os
# ==============================================================================
# Filename : configuration.py
# Purpose  :
# Author   : Yohann Li (lyh001686@sina.com)
# Date     : 2020-07-01 18:08
# ==============================================================================
import re
import sys

from PyQt5.QtCore import QSize
from PyQt5.QtGui import QColor, qRgb, qRgba

from conf.setLogger import GenerateLogger, log_folder


# ===============================================================================
# logging.basicConfig(level='DEBUG',
#                     format='%(asctime)s %(filename)-15s %(lineno)-3d %(funcName)-17s %(levelname)-5s | %(message)s')
# logger = logging
log_fp = os.path.join(log_folder, 'app.log')
logger = GenerateLogger(log_fp).set_logger(fp=log_fp, sh=True)
APPNAME = 'LogViewer'
VERSION = '2.1.6'
Built_Date = '2020.10.23'
ABOUT = f"""{APPNAME}({VERSION})
Built on {Built_Date}
"""

# App check update service URL
URL = 'http://1.1.1.1:8600/update'

# Project base path
if os.path.basename(os.path.dirname(sys.argv[0])) == 'MacOS':
    base_dir = os.path.dirname(os.path.dirname(sys.argv[0]))
else:
    base_dir = os.path.expanduser('~/PycharmProjects/Reader')

# different modes' identifier
DiagsPrompt = re.compile(r'(\[[\w:]*\] :-\) )')
OSPrompt = re.compile(r"~ root#|^login: $")
IBootPrompt = re.compile(r"^\] ")
ModesPromt = {'OS': OSPrompt, 'Recovery': IBootPrompt, 'Diags': DiagsPrompt}

# for tools
Dir_Identifier_in_Diags = ['<DIR>', '<SYMLINK>']
SYSD_PATH = ''  # sysdiagnose path
Astro_PATH = ''  # log

# Serial configuration
BAUDRATES = 115200
BYTESIZES = 8
PARITIES = 'N'
STOPBITS = 1
# 在此之上的这些变量应在使用之前根据自己的实际情况自行修改
# ===============================================================================

font_style = {'green': {'fgc': QColor(qRgb(40, 95, 23)),
                        'bgc': QColor(qRgb(206, 237, 208)),
                        'bold': True},
              'red': {'fgc': QColor(qRgb(144, 27, 21)),
                      'bgc': QColor(qRgb(247, 201, 206)),
                      'bold': True},
              'yellow': {'fgc': QColor(qRgb(147, 89, 29)),
                         'bgc': QColor(qRgb(252, 234, 166)),
                         'bold': True},
              'grey': {'fgc': QColor(qRgb(230, 230, 230)),
                       'bgc': QColor(qRgb(230, 230, 230)),
                       'bold': False},
              'lightyellow': {'fgc': QColor(qRgb(0, 0, 0)),
                              'bgc': QColor(qRgba(255, 255, 62, 255)),
                              'bold': True},
              'light': {'fgc': QColor('#000000'),
                        'bgc': QColor('#FFFFFF'),
                        'bold': False}
              }
color_pattern = {'passed': 'green',
                 'passing': 'green',
                 'pass': 'green',
                 'successfully': 'green',
                 'failure': 'red',
                 'failed': 'red',
                 'failing': 'red',
                 'fail': 'red',
                 'issue': 'red',
                 'error:': 'red',
                 'error': 'red',
                 'warning:': 'red',
                 'warn': 'red',
                 'warning': 'red',
                 'wave': 'yellow'
                 }
color_tuple = [(r'pass|success\w*', QColor(qRgb(40, 95, 23)), QColor(qRgb(206, 237, 208))),
               (r'fail|issue|error.*|warning.*', QColor(qRgb(144, 27, 21)), QColor(qRgb(247, 201, 206))),
               (r'wave', QColor(qRgb(147, 89, 29)), QColor(qRgb(252, 234, 166)))]
grey_tuple = ('grey', QColor(qRgb(230, 230, 230)), QColor(qRgb(230, 230, 230)))
light_tuple = ('light', QColor('#000000'), QColor('#FFFFFF'))
lightyellow_tuple = ('lightyellow', QColor('#000000'), QColor('#FFFFFF'))

text_color = dict(black=QColor('#000000'), yellow=QColor(qRgb(147, 89, 29)), green=QColor(qRgb(40, 95, 23)),
                  red=QColor(qRgb(144, 27, 21)))
back_color = {'green': QColor(136, 203, 107), 'red': QColor(qRgb(247, 201, 206)), 'yellow': QColor(qRgb(252, 234, 166))}

qss_path = os.path.join(base_dir, 'qss/qss')
img_path = os.path.join(base_dir, 'images')
tool_path = os.path.join(base_dir, 'tool')
APPICON = os.path.join(img_path, 'icon.icns')
APPPIC = os.path.join(img_path, 'app-iPad@2x.png')

with open(qss_path, 'r') as f:
    style = f.read()

ICON_SIZE = QSize(128, 128)
ReferNoteFile = os.path.join(tool_path, 'referenceNotes.html')
with open(ReferNoteFile, 'r') as rnf:
    ReferNotes = rnf.read()

# r_cur = re.compile(r"\x1b\[\d*C")
# l_cur = re.compile(r"\x1b\[\d*D")
cursor_pat = r"\x1b\[\d*[CD]"
color_pat = r"\x1b\[[\d;]*m"
bacspace_pat = r"\x08 \x08|\x08|\x07|\x1b\[K?"
re_cur_color_bac = re.compile(r'|'.join([color_pat, cursor_pat, bacspace_pat]))
l_r_cur = re.compile(r"\x1b\[\d*[CD]")
terminal_color_pattern = re.compile(r"\x1b\[[\d;]*m")
terminal_backspace_pat = re.compile(r"\x08 \x08|\x08|\x07|\x1b\[K?")
mode_mark = re.compile(r"(.*~ root# |.*\[[\w:]*\] :-\) |^\] )")
valid_cmd_pat = re.compile(r"\w")
valid_cmd_splitter = re.compile(r"\s")
replace_unconditionally = re.compile(br"\x07|\x08|\x1b\[[KDC]")
replace_unconditionally_str = re.compile(r"\x07|\x08|\x1b\[\d?[KDC]")
OS_Prefix = re.compile(r"(?P<osp>^.* root# $|^login: )")

line_background_color = QColor('#1f0000ff')
caret_width = 10
caret_line_visible = True
case_sensitivity = False

END_Signal = '->end<-'
USBFS_STDOUT_Path = os.path.join(log_folder, 'usbfs.log')
USBFS_STDERR_Path = os.path.join(log_folder, 'usbfs.err.log')
Grey_Base = QColor(qRgba(200, 200, 200, 100))
Normal_Base = QColor("#FFFFFF")

NotificationBoxDelaySeconds = 1
Key_Delay = 200
Read_Delay = 50
